<template>
  <el-card>
    <el-row>
      <el-col :span="12">
        <el-button
          type="primary"
          v-auth="'/sys/log/exportXls'"
          @click="exportXls()"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="12"></el-col>
    </el-row>
    <el-table border style="margin: 10px 0px" :data="list">
      <el-table-column
        label="ip"
        show-overflow-tooltip
        prop="ip"
      ></el-table-column>
      <el-table-column
        label="操作人"
        show-overflow-tooltip
        prop="logUserName"
      ></el-table-column>
      <el-table-column
        label="操作"
        show-overflow-tooltip
        prop="logName"
      ></el-table-column>
      <el-table-column
        label="操作时间"
        show-overflow-tooltip
        prop="createTime"
      ></el-table-column>
      <el-table-column
        label="参数"
        show-overflow-tooltip
        prop="oldParams"
      ></el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getPage"
      @size-change="handler"
    />
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
//引入请求
import { logGetPage, exportLog } from '@/api/system/log'
//引入ts类型
import { ElMessage } from 'element-plus'
//分页器当前页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(10)
let total = ref<number>(0)
let list = ref([])
//控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
//组件挂载完毕
onMounted(() => {
  getPage()
})
const getPage = async (pager = 1) => {
  //当前分页器的页码
  pageNo.value = pager
  let { status, data, msg } = await logGetPage({
    pageNo: pageNo.value,
    pageSize: pageSize.value,
  })
  if (status == 200) {
    let { records, total: tatalNums } = data
    total.value = tatalNums
    list.value = records
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}
//分页器下拉菜单发生变化触发
const handler = (pageSizes: number) => {
  getPage()
}

const exportXls = async () => {
  let ids = list.value.map((v) => v.id)
  let res = await exportLog({ ids: ids.join(',') })
  let url = URL.createObjectURL(new Blob([res]))
  let A = document.createElement('a')
  A.href = url
  let name = Date.now() + '.xlsx'
  A.setAttribute('download', name)
  document.body.appendChild(A)
  A.click()
}
</script>

<style scoped></style>
